//
//  OMShareViewController.m
//  Weibbo
//
//  Created by 李 林刚 on 13-8-11.
//  Copyright (c) 2013年 Omnivores. All rights reserved.
//

#import "OMShareViewController.h"
#import "ShareViewCell.h"
#import "ShareViewAdapter.h"

@interface OMShareViewController ()<ShareViewAdapterDelegate,ShareMangerDelegate>
{
    UITableView *myTableView;
    ShareViewAdapter *adapter;
    NSArray     *list;
    NSMutableArray  *messageArray;
    
    BOOL                  isSavePhoto;

    BOOL                   isShareSinaSuccess;
    BOOL                   isShareQZoneSuccess;
    
    BOOL                   isShareSinaFinished;
    BOOL                   isShareQZoneFinished;
}

@end

@implementation OMShareViewController
@synthesize delegate;

@synthesize shareImage;

-(void)dealloc
{
    UIRelease(myTableView);
    UIRelease(adapter);
    UIRelease(list);
    UIRelease(messageArray);
    UIRelease(shareImage);
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        messageArray  = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    isSavePhoto = NO;
    list = [[NSArray alloc]initWithObjects:@"新浪微博",@"QQ空间",@"相册", nil];
    adapter = [[ShareViewAdapter alloc]initWithList:list andAdapter:self];
    myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,250, 144) style:UITableViewStylePlain];
    [myTableView setTableAdapter:adapter];
    [self.view addSubview:myTableView];
    
    UIButton  *shareButton = [[UIButton alloc]initWithFrame:CGRectMake(21, CGRectGetHeight(myTableView.frame)+22,216, 34)];
    [shareButton addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    [shareButton setImage:[UIImage imageNamed:@"Btn_share_normal"] forState:UIControlStateNormal];
    [shareButton setImage:[UIImage imageNamed:@"Btn_share_select"] forState:UIControlStateHighlighted];
    [self.view addSubview:shareButton];
    [shareButton release];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[ShareManger SharedInstance]setDelegate:nil];
}

- (void)share
{
//    if (![[ShareManger SharedInstance] IsBind:ShareTypeSina]&&![[ShareManger SharedInstance] IsBind:ShareTypeTencentOAuth]) {
//        UIAlertView *warningView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请绑定账号后再分享" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [warningView show];
//        [warningView release];
//        return;
//    }
//    [AlertView ShowWindowMBProgressMessage:@"请稍候..." OffsetX:0 OffsetY:0];
    
    if (isSavePhoto) {
        UIImageWriteToSavedPhotosAlbum(shareImage, nil, nil, nil);
    }
    return;
    NSArray  *cells = [myTableView visibleCells];
    isShareQZoneSuccess = YES;
    isShareSinaSuccess = YES;
    [[ShareManger SharedInstance] setDelegate:self];
    for(ShareViewCell *cell in cells)
    {
        if ([cell isKindOfClass:[ShareViewCell class]])
        {
            if (cell.tag == ShareTypeSina) {
                if (cell.willSend) {
                    [[ShareManger SharedInstance]ShareMessageTitle:@"" ShareImage:shareImage ImageURL:nil ShareType:ShareTypeSina];
                    [[ShareManger SharedInstance]setDelegate:self];
                    isShareSinaSuccess = NO;
                    isShareSinaFinished = NO;
                }else{
                    isShareSinaSuccess = YES;
                    isShareSinaFinished = YES;
                }
            }
          if (cell.tag == ShareTypeTencentOAuth ) {
                if (cell.willSend) {
                    [[ShareManger SharedInstance]ShareMessageTitle:@"" ShareImage:shareImage ImageURL:nil ShareType:ShareTypeTencentOAuth];
                    [[ShareManger SharedInstance]setDelegate:self];
                    isShareQZoneSuccess = NO;
                    isShareQZoneFinished = NO;
                }else{
                    isShareQZoneSuccess = YES;
                    isShareQZoneFinished = YES;
                }
            }
        }
    }
}

-(void)viewDidUnload
{
    UIRelease(myTableView);
    UIRelease(adapter);
    UIRelease(list);
    UIRelease(messageArray);
    [super viewDidUnload];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --------------ShareViewAdapterDelegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0://新浪微博
        {
            if (![[ShareManger SharedInstance]IsBind:ShareTypeSina])
            {
               [[ShareManger SharedInstance]LogInWithLogInType:ShareTypeSina];
               [[ShareManger SharedInstance]setDelegate:self];
            }
  
            else
            {
                [[ShareManger SharedInstance]LoginOut:ShareTypeSina];
                [[ShareManger SharedInstance]setDelegate:self];
            }
        }
            break;
        case 1://QQ空间
        {
            if (![[ShareManger SharedInstance]IsBind:ShareTypeTencentOAuth])
            {
                [[ShareManger SharedInstance]LogInWithLogInType:ShareTypeTencentOAuth];
                [[ShareManger SharedInstance]setDelegate:self];
            }
            else
            {
                [[ShareManger SharedInstance]LoginOut:ShareTypeTencentOAuth];
                [[ShareManger SharedInstance]setDelegate:self];
            }
        }
        default:
            break;
    }
}

-(void)ShareViewAdapterDelegateSaveToAlbum:(BOOL)isSave
{
    isSavePhoto = isSave;
}

#pragma mark  ---ShareMangerDelegate
-(void)ShareMangerShareType:(ShareType)type DidLoginIn:(BOOL)IsLogin
{
    if (IsLogin) {
        [AlertView ShowAlterViewTitle:nil Message:type==ShareTypeSina?@"Sina绑定成功":@"Qzone绑定成功"];
        [myTableView reloadData];
    }
    else
    {
        [AlertView ShowAlterViewTitle:nil Message:type==ShareTypeSina?@"Sina绑定失败":@"Qzone绑定失败"];
    }
}

-(void)ShareMangerShareType:(ShareType)type DidLoginOut:(BOOL)IsOut
{
    if (IsOut) {
        [AlertView ShowAlterViewTitle:nil Message:type==ShareTypeSina?@"Sina解除绑定成功":@"Qzone解除绑定成功"];
        [myTableView reloadData];
    }
    else
    {
        [AlertView ShowAlterViewTitle:nil Message:type==ShareTypeSina?@"Sina解除绑定失败":@"Qzone解除绑定失败"];
    }
}

-(void)ShareMangerShareType:(ShareType)type DidShareMessage:(BOOL)IsSuccess
{
    if (type == ShareTypeTencentOAuth) {
        isShareQZoneSuccess = IsSuccess;
        isShareQZoneFinished = YES;
        if (IsSuccess) {
            [messageArray addObject:@"QZone 分享成功"];
        }else{
            [messageArray addObject:@"QZone 分享失败"];
        }
    }
    if (type == ShareTypeSina) {
        isShareSinaSuccess = IsSuccess;
        isShareSinaFinished = YES;
        if (IsSuccess) {
            [messageArray addObject:@"Sina 分享成功"];
        }else{
            [messageArray addObject:@"Sina 分享失败"];
        }
    }
    if (isShareQZoneFinished && isShareSinaFinished) {
        NSMutableString  *message = [NSMutableString string];
        for(NSString  *m in messageArray)
        {
            [message appendString:m];
            [message appendString:@"\n"];
        }
        [messageArray removeAllObjects];
        
        [AlertView ShowWindowMBProgressMessage:message];
        
        [self performSelector:@selector(HidenMBProgress) withObject:nil afterDelay:2];
    }
}

-(void)HidenMBProgress
{
    [AlertView HidenMBProgressFromWindow];
       if ([delegate respondsToSelector:@selector(ShareViewDidDismiss)]) {
           [delegate ShareViewDidDismiss];
       }
}
@end
